package com.example.demo.controller;

import com.example.demo.entity.Appointment;
import com.example.demo.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/appointment")
@CrossOrigin
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    // 新增预约接口
    @PostMapping("/add")
    public Map<String, Object> addAppointment(@RequestBody Appointment appointment) {
        Map<String, Object> result = new HashMap<>();

        // 如果前端传的appointmentTime是字符串格式，这里可以添加转换，或确保前端传LocalDateTime格式
        // 这里假设前端传的是ISO标准时间字符串，Spring自动转换

        // 设置创建时间
        appointment.setCreatedAt(java.time.LocalDateTime.now());

        boolean saved = appointmentService.save(appointment);
        if (saved) {
            result.put("success", true);
            result.put("message", "预约成功");
        } else {
            result.put("success", false);
            result.put("message", "预约失败");
        }
        return result;
    }

    // 获取医生今日预约
    @GetMapping("/today")
    public List<Appointment> getTodayAppointments(@RequestParam Integer doctorId) {
        return appointmentService.getTodayAppointments(doctorId);
    }

    // 叫号
    @PostMapping("/call")
    public Map<String, Object> call(@RequestBody Map<String, Integer> map) {
        boolean updated = appointmentService.updateStatus(map.get("id"), "呼叫中");
        return buildStatusResult(updated, "叫号");
    }

    // 开始接诊
    @PostMapping("/start")
    public Map<String, Object> start(@RequestBody Map<String, Integer> map) {
        boolean updated = appointmentService.updateStatus(map.get("id"), "就诊中");
        return buildStatusResult(updated, "开始接诊");
    }

    // 接诊结束
    @PostMapping("/end")
    public Map<String, Object> end(@RequestBody Map<String, Integer> map) {
        boolean updated = appointmentService.updateStatus(map.get("id"), "完毕");
        return buildStatusResult(updated, "接诊结束");
    }

    // 患者根据预约ID查询状态
    @GetMapping("/patientStatus")
    public Map<String, String> getPatientStatus(@RequestParam Integer id) {
        Appointment appointment = appointmentService.getById(id);
        Map<String, String> map = new HashMap<>();
        map.put("status", appointment != null ? appointment.getStatus() : "未知");
        return map;
    }

    // 新增：根据患者姓名获取预约列表（“我的预约”接口）
    @GetMapping("/my")
    public List<Appointment> getAppointmentsByPatientName(@RequestParam String patientName) {
        return appointmentService.getAppointmentsByPatientName(patientName);
    }

    // 工具方法：封装状态更新接口返回结果
    private Map<String, Object> buildStatusResult(boolean success, String action) {
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("success", true);
            result.put("message", action + "成功");
        } else {
            result.put("success", false);
            result.put("message", action + "失败");
        }
        return result;
    }
}
